//
//  MyPresentationViewController.m
//  JiongTu
//
//  Created by 杨璟 on 16/4/22.
//  Copyright © 2016年 YSKJ. All rights reserved.
//

#define VIEW_W 266
#define VIEW_H 371

#import "MyPresentationViewController.h"

@interface MyPresentationViewController ()

@property (nonatomic, strong) UIView *dimmingView;

@end

@implementation MyPresentationViewController

- (instancetype)initWithPresentedViewController:(UIViewController *)presentedViewController presentingViewController:(UIViewController *)presentingViewController
{
    if ((self = [super initWithPresentedViewController:presentedViewController presentingViewController:presentingViewController])) {
        _dimmingView = [[UIView alloc] init];
        _dimmingView.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.5];
        _dimmingView.alpha = 0;
        UITapGestureRecognizer *hidssTouch = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(hidssViewTouch)];
        [_dimmingView addGestureRecognizer:hidssTouch];
    }
    return self;
}

//点击透明部分视图消失时间
-(void)hidssViewTouch{
    if (self.hidssView) {
        self.hidssView();
    }
}

//页面进入的时候调用
- (void)presentationTransitionWillBegin
{
    _dimmingView.frame = self.containerView.bounds;
    [self.containerView addSubview:_dimmingView];
    [self.containerView addSubview:self.presentedView];
    
    id<UIViewControllerTransitionCoordinator> coordinator = self.presentingViewController.transitionCoordinator;
    [coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext>  _Nonnull context) {
        _dimmingView.alpha = 1;
    } completion:^(id<UIViewControllerTransitionCoordinatorContext>  _Nonnull context) {
        
    }];
}

//页面消失的时候调用
- (void)presentationTransitionDidEnd:(BOOL)completed
{
    if (!completed) {
        [_dimmingView removeFromSuperview];
    }
}

- (void)dismissalTransitionWillBegin
{
    id<UIViewControllerTransitionCoordinator> coordinator = self.presentingViewController.transitionCoordinator;
    [coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext>  _Nonnull context) {
        _dimmingView.alpha = 0.0;
    } completion:^(id<UIViewControllerTransitionCoordinatorContext>  _Nonnull context) {
        
    }];
}

- (void)dismissalTransitionDidEnd:(BOOL)completed
{
    if (completed) {
        [_dimmingView removeFromSuperview];
    }
}

//改变页面的frame
- (CGRect)frameOfPresentedViewInContainerView
{
    CGRect frame = self.containerView.bounds;
    CGFloat width = ([UIScreen mainScreen].bounds.size.width - VIEW_W)/2;
    CGFloat height = ([UIScreen mainScreen].bounds.size.height - VIEW_H) / 2;
    frame = CGRectInset(frame, width, height);
    return frame;
}

- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
    [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
    
    [coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext>  _Nonnull context) {
        _dimmingView.frame = self.containerView.bounds;
    } completion:nil];
}

@end
